package com.xyf.sys.service.impl;

import com.xyf.sys.domain.Role;
import com.xyf.sys.domain.User;
import com.xyf.sys.domain.UserRole;
import com.xyf.sys.mapper.UserRoleMapper;
import com.xyf.sys.service.IRoleService;
import com.xyf.sys.service.IUserRoleService;
import com.xyf.sys.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class UserRoleServiceImpl implements IUserRoleService {

    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IUserService userService;

    @Override
    public List<Role> getRoleList(String userId) {
        User user = userService.get(userId);
        List<UserRole> userRoles = userRoleMapper.selectDeptRole(userId, user.getDeptId());
        if (CollectionUtils.isEmpty(userRoles)){
            return null;
        }
        return userRoles.stream().map(userRole -> roleService.getRole(userRole.getRoleId())).collect(Collectors.toList());
    }

    @Override
    public List<Role> getRoleList(String userId, Long deptId) {
        List<UserRole> userRoles = userRoleMapper.selectDeptRole(userId, deptId);
        if (CollectionUtils.isEmpty(userRoles)){
            return null;
        }
        return userRoles.stream().map(userRole -> roleService.getRole(userRole.getRoleId())).collect(Collectors.toList());
    }
}
